package com.example.demo.config;

import com.example.demo.Interceptor.loginHandlerInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class Config implements WebMvcConfigurer
{
    @Value("${goodsPicPathPattern}")
    private String picturePathPattern;
    @Value("${iconPathPattern}")
    private String iconPathPattern;
    @Value("${uploadGoodsPath}")
    private String uploadGoodsPath;
    @Value("${uploadIconPath}")
    private String uploadIconPath;
    @Override
    public void addViewControllers(ViewControllerRegistry registry)
    {
        registry.addViewController("/upload").setViewName("/admin/upload");
        registry.addViewController("/enroll").setViewName("/show/enroll");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        registry.addResourceHandler(picturePathPattern+"**").addResourceLocations("file:"+uploadGoodsPath);
        registry.addResourceHandler(iconPathPattern+"**").addResourceLocations("file:"+uploadIconPath);
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new loginHandlerInterceptor()).addPathPatterns("/show/order/cart","/show/address/choose/7",
                "/show/address/add/7","/dashboard","/show/order/list","/orders","/returns","/historyOrders","/goods").excludePathPatterns("/static/**");
    }
}
